<?php

namespace app\common\model;
/*
 *  邀请码列表
 * */

use think\Model;

class ShareCodeList extends Model
{
    /*
     *  获取邀请码
     * */
    public function getShareCode()
    {
        $i = 5;
        $share_code = '';
        $userModel = new \app\common\model\User();
        while ($i > 0) {
            $i--;
            $share_code1 = $this->value('share_code');
            if (empty($share_code1)) continue;
            $this->where('share_code', $share_code1)->delete();
            if (!$userModel->where('share_code', $share_code1)->count()) {
                $share_code = $share_code1;
                break;
            }
        }
        return $share_code;
    }

    /*
     *  生成邀请码
     * */
    public function createShareCode($pre_num)
    {
        $min_num = $pre_num * 10000 + 1;
        $max_num = ($pre_num + 1) * 10000;
        $a = [];
        for ($i = $min_num; $i < $max_num; $i++) $a[] = $i / 10000;
        shuffle($a);
        $list = [];
        foreach ($a as $k => $v) {
            $list[] = [
                'share_code' => $v
            ];
        }
        $this->insertAll($list);
        return true;
    }


}